﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PloobsEngine.Components;
using Microsoft.Xna.Framework;

namespace PloobsEngine.Features
{
    public class FPSCounter : BasicComponent
    {

        private float _TotalTime = 0f, _TotalTimeX = 0f,
        _Fps = 0f,
        _FpsX = 0f;
        private  float _DisplayFPS;
        private float _UpdateFPS;

        public float FPS
        {
            get { return _UpdateFPS + _DisplayFPS; }           
        }


        public float UpdateFPS
        {
            get { return _UpdateFPS; }            
        }

        public float DrawFPS
        {
            get { return _DisplayFPS; }
        }
 

        public FPSCounter(EngineStuff game)
         {
           
        
        }
        public override ComponentType ComponentType
        {
            get { return ComponentType.POS_DRAWABLE_AND_UPDATEABLE; }
        }

        public static readonly String MyName = "FPSCounter";
        public override string getMyName()
        {
            return MyName;
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void AfterDraw(Microsoft.Xna.Framework.GameTime gt, Matrix activeView, Matrix activeProjection)
        {
            _TotalTime += (float)gt.ElapsedRealTime.TotalSeconds;
            _Fps += 1;

            if (_TotalTime >= 1.0f)
            {
                _TotalTime = _TotalTime - (float)((int)_TotalTime);
                _DisplayFPS = _Fps;
                _Fps = 0;                
            }          
            
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gt)
        {
            _TotalTimeX += (float)gt.ElapsedRealTime.TotalSeconds;
            _FpsX += 1;

            if (_TotalTimeX >= 1.0f)
            {
                _TotalTimeX = _TotalTimeX - (float)((int)_TotalTimeX);
                _UpdateFPS = _FpsX;
                _FpsX = 0;
            }           
            
            base.Update(gt);
        }
        
    }
}
